package com.example.demo.service.order;

import com.example.demo.common.IsObjectEmpty;
import com.example.demo.domain.order.SaleOrder;
import com.example.demo.domain.order.SaleOrderItem;
import com.example.demo.domain.tovip.Tovip;
import com.example.demo.domain.vip.Vip;
import com.example.demo.mapper.order.SaleOrderItemMapper;
import com.example.demo.mapper.order.SaleOrderMapper;
import com.example.demo.mapper.service.ServiceMapper;
import com.example.demo.mapper.tovip.TovipMapper;
import com.example.demo.mapper.vip.VipMapper;
import com.example.demo.service.vip.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.demo.domain.service.Service;



import java.math.BigDecimal;

@org.springframework.stereotype.Service
public class SaleOrderItemServiceImpl implements SaleOrderItemService{
	@Autowired
	private SaleOrderItemMapper saleOrderItemMapper;

	@Autowired
	private VipMapper vipMapper;

	@Autowired
	private ServiceMapper serviceMapper;

	@Autowired
	private TovipMapper tovipMapper;

	@Autowired
	private SaleOrderMapper saleOrderMapper;



	@Override
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(SaleOrder saleOrder,Integer[] serviceId) {
		// TODO Auto-generated method stub
		BigDecimal[] result = new BigDecimal[2];
		//根据会员卡id查询会员类型
		if(saleOrder.getVipId()!=null&&saleOrder.getVipId()!=0) {
			Vip vip = vipMapper.selectByPrimaryKey(saleOrder.getVipId());
			for (Integer id : serviceId) {
				SaleOrderItem saleOrderItem = new SaleOrderItem();
				saleOrderItem.setSaleorderno(saleOrder.getSaleorderno());
				//计算订单明细的价格
				saleOrderItem.setSubprice(calculateSubPrice(id,saleOrder.getVipId()));
				saleOrderItem.setServiceId(id);
				//插入订单明细
				insertSelective(saleOrderItem);
			}
			//计算订单中的价格
			result = calculatePrice(serviceId,saleOrder.getVipId());
			saleOrder.setReceivable(result[0]);
			saleOrder.setTotal(result[1]);
		}
		else{
			for (Integer id : serviceId) {
				SaleOrderItem saleOrderItem = new SaleOrderItem();
				saleOrderItem.setSaleorderno(saleOrder.getSaleorderno());
				//计算订单明细的价格
				saleOrderItem.setSubprice(calculateSubPrice(id,0));
				saleOrderItem.setServiceId(id);
				insertSelective(saleOrderItem);
			}
			result = calculatePrice(serviceId,saleOrder.getVipId());
			saleOrder.setReceivable(result[0]);
			saleOrder.setTotal(result[1]);
		}
		//插入订单中
		return this.saleOrderMapper.insertSelective(saleOrder);
	}

	@Override
	public int insertSelective(SaleOrderItem saleOrderItem) {

		// TODO Auto-generated method stub
		return this.saleOrderItemMapper.insertSelective(saleOrderItem);
	}

	@Override
	public SaleOrderItem selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return this.saleOrderItemMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(SaleOrder saleOrder, Integer[] serviceId) {
		// TODO Auto-generated method stub
		//计算价格
		BigDecimal[] price = calculatePrice(serviceId,saleOrder.getVipId());
		//将原价和实际价格放入订单中
		saleOrder.setReceivable(price[0]);
		saleOrder.setTotal(price[1]);
		//先将该订单编号下的明细删除
		saleOrderItemMapper.deleteBySaleOrderNo(saleOrder.getSaleorderno());
		//更新订单明细
		for(Integer id :serviceId){
			SaleOrderItem saleOrderItem = new SaleOrderItem();
			saleOrderItem.setSaleorderno(saleOrder.getSaleorderno());
			//再重新插入新的记录
			saleOrderItem.setSubprice(calculateSubPrice(id,saleOrder.getVipId()));
			saleOrderItem.setServiceId(id);
			System.out.println(saleOrderItem.getServiceId()+" "+saleOrderItem.getSubprice()+
			" "+saleOrderItem.getSaleorderno());
			insertSelective(saleOrderItem);
		}
		//更新账单信息
		return saleOrderMapper.updateByPrimaryKeySelective(saleOrder) ;
	}
	//计算订单明细中的价格
	@Override
	public BigDecimal calculateSubPrice(Integer serviceId,Integer vipId){
		Service service = serviceMapper.selectByPrimaryKey(serviceId);
		BigDecimal result = service.getServicePrice();
		if(vipId == null || vipId==0){
			return result;
		}
		else{
		Vip vip = vipMapper.selectByPrimaryKey(vipId);
		//如果查询出来的结果不为空或者会员并没有过期
		if (!IsObjectEmpty.checkObjAllFieldsIsNull(vip)&&vip.getVipState()!=2) {
			Tovip tovip = tovipMapper.selectByPrimaryKey(vip.getTovipId());
			result = result.multiply(tovip.getTovipDisc());
		}
		return result;
		}
	}
	//计算订单中的价格
	@Override
	public BigDecimal[] calculatePrice(Integer[] servicesIds, Integer vipId) {
		BigDecimal[] result = new BigDecimal[2];
		BigDecimal orginal = new BigDecimal("0");
		if(vipId == null || vipId == 0){
			for (Integer id : servicesIds) {
				Service service = serviceMapper.selectByPrimaryKey(id);
				BigDecimal temp = service.getServicePrice();
				orginal = orginal.add(temp);
			}
			result[0] = orginal;
			result[1] = orginal;
			return result;
		}
		else{
		//根据会员卡id查询会员类型
		Vip vip = vipMapper.selectByPrimaryKey(vipId);

		for (Integer id : servicesIds) {
			Service service = serviceMapper.selectByPrimaryKey(id);
			BigDecimal temp = service.getServicePrice();
			orginal = orginal.add(temp);
		}
		result[0] = orginal;
		if (!IsObjectEmpty.checkObjAllFieldsIsNull(vip)&&vip.getVipState()!=2) {
			Tovip toVip = tovipMapper.selectByPrimaryKey(vip.getTovipId());
			result[1] = result[0].multiply(toVip.getTovipDisc());
		} else {
			result[1] = result[0];
		}
		return result;
		}
	}


	@Override
	public int updateByPrimaryKey(SaleOrderItem saleOrderItem) {
		// TODO Auto-generated method stub
		return this.updateByPrimaryKey(saleOrderItem);
	}
	

}
